home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Applications / gdbm-1.7.3 / testdbm.c < prev    next >
Text File  |  1994-05-21  |  5KB  |  216 lines

  1. /* testdbm.c - Driver program to test the dbm interface routines. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.        
  27. *************************************************************************/
  28.  
  29.  
  30. /* include system configuration before all else. */
  31. #include "autoconf.h"
  32.  
  33. #include <stdio.h>
  34. #include <sys/types.h>
  35. #if HAVE_SYS_FILE_H
  36. #include <sys/file.h>
  37. #endif
  38. #include <sys/stat.h>
  39.  
  40. #define TRUE  1
  41. #define FALSE 0
  42.  
  43. typedef struct {
  44.   char *dptr;
  45.   int   dsize;
  46. } datum;
  47.  
  48. extern datum fetch ();
  49. extern datum firstkey ();
  50. extern datum nextkey ();
  51.  
  52. /* The test program allows one to call all the routines plus the hash function.
  53.    The commands are single letter commands.  The user is prompted for all other
  54.    information.  The commands are q (quit), f (fetch), s (store), d (delete),
  55.    1 (firstkey), n (nextkey) and h (hash function). */
  56.  
  57. int
  58. main (argc, argv)
  59.      int argc;
  60.      char *argv[];
  61. {
  62.  
  63.   char  cmd_ch;
  64.  
  65.   datum key_data;
  66.   datum data_data;
  67.   datum return_data;
  68.  
  69.   char key_line[500];
  70.   char data_line[1000];
  71.  
  72.   char done = FALSE;
  73.   char sys[255];
  74.  
  75.   char *file_name;
  76.  
  77.   /* Argument checking. */
  78.   if (argc > 2)
  79.     {
  80.       printf ("Usage: %s [dbm-file] \n",argv[0]);
  81.       exit (2);
  82.     }
  83.  
  84.   if (argc > 1)
  85.     {
  86.       file_name = argv[1];
  87.     }
  88.   else
  89.     {
  90.       file_name = "junkdbm";
  91.     }
  92.  
  93.   /* Initialize */
  94.   data_data.dptr = data_line;
  95.  
  96.   if (dbminit (file_name) != 0)
  97.     {
  98.       sprintf (sys,"touch %s.pag %s.dir", file_name, file_name);
  99.       system (sys);
  100.       if (dbminit (file_name) != 0)
  101.     {
  102.       printf ("dbminit failed.\n");
  103.       exit (2);
  104.     }
  105.     }
  106.  
  107.   /* Welcome message. */
  108.   printf ("\nWelcome to the dbm test program.  Type ? for help.\n\n");
  109.   
  110.   while (!done)
  111.     {
  112.       printf ("com -> ");
  113.       cmd_ch = getchar ();
  114.       while (getchar () != '\n') /* Do nothing. */;
  115.       switch (cmd_ch)
  116.     {
  117.     case 'q':
  118.       done = TRUE;
  119.       break;
  120.  
  121.     case 'f':
  122.       printf ("key -> ");
  123.       gets (key_line);
  124.       key_data.dptr = key_line;
  125.       key_data.dsize = strlen (key_line)+1;
  126.       return_data = fetch (key_data);
  127.       if (return_data.dptr != NULL)
  128.           printf ("data is ->%s\n\n", return_data.dptr);
  129.       else
  130.         printf ("No such item found.\n\n");
  131.       break;
  132.  
  133.     case 's':
  134.       printf ("key -> ");
  135.       gets (key_line);
  136.       key_data.dptr = key_line;
  137.       key_data.dsize = strlen (key_line)+1;
  138.       printf ("data -> ");
  139.       gets (data_line);
  140.       data_data.dsize = strlen (data_line)+1;
  141.       if (store (key_data, data_data) != 0)
  142.         printf ("Item not inserted. \n");
  143.       printf ("\n");
  144.       break;
  145.  
  146.     case 'd':
  147.       printf ("key -> ");
  148.       gets (key_line);
  149.       key_data.dptr = key_line;
  150.       key_data.dsize = strlen (key_line)+1;
  151.       if (delete (key_data) != 0)
  152.         printf ("Item not found or deleted\n");
  153.       printf ("\n");
  154.       break;
  155.  
  156.     case '1':
  157.       key_data = firstkey ();
  158.       if (key_data.dptr != NULL)
  159.         {
  160.           return_data = fetch (key_data);
  161.           printf ("key  is ->%s\n", key_data.dptr);
  162.           printf ("data is ->%s\n\n", return_data.dptr);
  163.         }
  164.       else
  165.         printf ("No such item found.\n\n");
  166.       break;
  167.  
  168.  
  169.     case '2':
  170.       key_data = nextkey (key_data);
  171.       if (key_data.dptr != NULL)
  172.         {
  173.           return_data = fetch (key_data);
  174.           printf ("key  is ->%s\n", key_data.dptr);
  175.           printf ("data is ->%s\n\n", return_data.dptr);
  176.         }
  177.       else
  178.         printf ("No such item found.\n\n");
  179.       break;
  180.  
  181.     case 'c':
  182.       {
  183.         int temp;
  184.         temp = 0;
  185.         return_data = firstkey ();
  186.         while (return_data.dptr != NULL)
  187.           {
  188.         temp++;
  189.         return_data = nextkey (return_data);
  190.           }
  191.         printf ("There are %d items in the database.\n\n", temp);
  192.       }
  193.       break;
  194.  
  195.     case '?':
  196.       printf ("c - count elements\n");
  197.       printf ("d - delete\n");
  198.       printf ("f - fetch\n");
  199.       printf ("q - quit\n");
  200.       printf ("s - store\n");
  201.       printf ("1 - firstkey\n");
  202.       printf ("2 - nextkey on last return value\n\n");
  203.       break;
  204.  
  205.     default:
  206.       printf ("What? \n\n");
  207.       break;
  208.  
  209.     }
  210.     }
  211.  
  212.   /* Quit normally. */
  213.   exit (0);
  214.  
  215. }
  216.